<template>
<div class="prodetail">
  <!-- 加载状态 -->
  <div v-if="loading" class="loading-container">
    <van-loading type="spinner" color="#1989fa">加载中...</van-loading>
  </div>

  <!-- 错误提示 -->
  <div v-else-if="error" class="error-container">
    <p>{{ error }}</p>
    <van-button type="primary" size="small" @click="getDetail">重新加载</van-button>
  </div>

  <!-- 商品详情内容 -->
  <div v-else-if="detail && detail.id">
    <!-- 商品标题 -->
    <h1 class="product-title">{{ detail.name }}</h1>

    <!-- 商品价格 -->
    <div class="product-price">¥{{ detail.price ? detail.price.toFixed(2) : '0.00' }}</div>

    <!-- 商品图片 -->
    <div class="product-image">
      <img :src="detail.image" :alt="detail.name" />
    </div>

    <!-- 商品描述 -->
    <div class="product-description" v-if="detail.description">
      {{ detail.description }}
    </div>

    <!-- 商品销量 -->
    <div class="product-sales" v-if="detail.sales">
      已售 {{ detail.sales }} 件
    </div>
  </div>

  <!-- 底部导航栏 -->
  <div class="bottom-nav">
    <div class="nav-left">
      <div class="nav-item" @click="goToHome">
        <van-icon name="home-o" size="24" />
        <span>首页</span>
      </div>
      <div class="nav-item" @click="goToCart">
        <van-icon name="cart-o" size="24" />
        <span>购物车</span>
        <!-- 购物车数量角标 -->
        <van-badge :content="cartCount" v-if="cartCount > 0" />
      </div>
    </div>
    <div class="nav-right">
      <van-button type="default" size="small" @click="addToCart">加入购物车</van-button>
      <van-button type="primary" size="small" @click="buyNow">立即购买</van-button>
    </div>
  </div>
</div>
</template>

<script>
import { productAPI } from '@/api/index';
export default {
  name: 'ProDetailIndex',
  data() {
    return {
      detail: {},
      loading: false,
      error: null,
      cartCount: 0 // 购物车数量
    }
  },
  // 创建getDetail方法，统一管理商品详情获取逻辑
  methods: {
    /**
     * 获取商品详情数据
     * 从路由参数中获取商品ID并调用API获取详情
     */
    getDetail() {
      const id = this.$route.params.id;
      if (!id) {
        this.error = '商品ID不存在';
        return;
      }

      this.loading = true;
      this.error = null;

      productAPI.getDetail(id)
        .then(res => {
          this.loading = false;
          if (res && res.code === 200) {
            this.detail = res.data;
          } else {
            this.error = res.message || '获取商品详情失败';
          }
        })
        .catch(err => {
          this.loading = false;
          this.error = '网络错误，请稍后重试';
          console.error('商品详情请求失败:', err);
        });
    },

    /**
     * 返回首页
     */
    goToHome() {
      this.$router.push('/');
    },

    /**
     * 跳转到购物车页面
     */
    goToCart() {
      // 这里假设购物车页面路由为'/cart'
      this.$router.push('/cart');
    },

    /**
     * 加入购物车
     */
    addToCart() {
      if (!this.detail || !this.detail.id) {
        this.$toast('商品信息未加载完成');
        return;
      }

      // 这里可以调用添加到购物车的API
      // 为演示目的，简单模拟添加到购物车
      this.$toast.success('已加入购物车');
      this.cartCount += 1;

      // 实际项目中应该调用API
      // cartAPI.addToCart(this.detail.id)
      //   .then(res => {
      //     this.$toast.success('已加入购物车');
      //     // 更新购物车数量
      //   })
      //   .catch(err => {
      //     this.$toast.fail('加入购物车失败');
      //   });
    },

    /**
     * 立即购买
     */
    buyNow() {
      if (!this.detail || !this.detail.id) {
        this.$toast('商品信息未加载完成');
        return;
      }

      // 跳转到订单确认页面，携带商品信息
      this.$router.push({
        path: '/order/confirm',
        query: { productId: this.detail.id, quantity: 1 }
      });
    }
  },
  created() {
    // 直接调用getDetail方法获取详情
    this.getDetail();
    // 可以从localStorage或Vuex中获取购物车数量
    // 这里为演示目的设置为0
  },
  // 路由参数变化监听
  watch: {
    '$route.params.id': {
      handler() {
        this.getDetail();
      },
      immediate: true
    }
  }
}
</script>

<style scoped>
.prodetail {
  padding: 15px;
  background-color: #fff;
  /* 为底部导航栏留出空间 */
  padding-bottom: 60px;
  min-height: 100vh;
  box-sizing: border-box;
}

.loading-container,
.error-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.error-container p {
  margin-bottom: 15px;
  color: #999;
}

.product-title {
  font-size: 18px;
  margin-bottom: 10px;
  line-height: 1.4;
}

.product-price {
  font-size: 20px;
  color: #ee0a24;
  font-weight: bold;
  margin-bottom: 15px;
}

.product-image {
  margin-bottom: 15px;
}

.product-image img {
  width: 100%;
  height: auto;
  border-radius: 8px;
}

.product-description {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 10px;
}

.product-sales {
  font-size: 12px;
  color: #999;
}

/* 底部导航栏样式 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.nav-left {
  display: flex;
  align-items: center;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-right: 20px;
  cursor: pointer;
  position: relative;
}

.nav-item span {
  font-size: 12px;
  margin-top: 2px;
  color: #666;
}

.nav-right {
  display: flex;
  align-items: center;
}

.nav-right .van-button {
  margin-left: 10px;
  padding: 0 15px;
  height: 32px;
  line-height: 32px;
  font-size: 14px;
}
</style>
